package com.situ.onlinebookstore.front.controller.api;

import com.situ.onlinebookstore.common.entity.Goods;
import com.situ.onlinebookstore.common.vo.Result;
import com.situ.onlinebookstore.front.service.GoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/goods")
@RequiredArgsConstructor
public class GoodsApiController {
    private final GoodsService goodsService;

    @GetMapping("")
    private Result search(Integer page, Integer limit, Goods goods) {
        if (page == null) {
            return Result.ok(goodsService.get(goods));
        } else {
            return Result.ok(goodsService.getForPage(page, limit, goods));
        }
    }

    // api/goods/10
    @GetMapping("/{id}")  //路径参数
    public Result get(@PathVariable("id") Integer id){
        return Result.ok(goodsService.getById(id));
    }
}
